package com.ws.webpacket.activity.dialog;

import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.ws.webpacket.R;
import com.ws.webpacket.internet.been.B_RootList;
import com.ws.webpacket.util.HLog;
import com.ws.webpacket.util.SharedPerformanceUtile;


public class UpDataDialog   extends DialogFragment {

    private TextView t_Tips;
    private CheckBox c_Check;
    private Button b_Cancel,b_Allow;

    private B_RootList rootList;

    private DownLoadListener downLoadListener;

    public UpDataDialog (B_RootList rootList){
        this.rootList = rootList;
        HLog.I(rootList.getDef().get(0));
    }

    private void initView(View view){
        t_Tips = view.findViewById(R.id.textView_UpDataDialog_Tips);
        c_Check = view.findViewById(R.id.CheckBox_UpDataDialog);
        b_Cancel = view.findViewById(R.id.button_UpDataDialog_Cancel);
        b_Cancel.setOnClickListener(onClickListener);
        b_Allow = view.findViewById(R.id.button_UpDataDialog_Allow);
        b_Allow.setOnClickListener(onClickListener);
    }

    private void initViewInfo(){
        if(rootList==null)return;
            t_Tips.setText(rootList.getContent());

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.fragment_updata,null);
        initView(container);

        initViewInfo();
        return container;
    }


    @Override
    public void onStop() {
        super.onStop();
    }

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int id= v.getId();
            switch (id){
                case R.id.button_UpDataDialog_Cancel:
                    if(c_Check.isChecked()){
                        SharedPerformanceUtile.put(getContext(),SharedPerformanceUtile.SAVE_STATUS,true);
                    }
                    dismiss();
                    break;
                case R.id.button_UpDataDialog_Allow:
                    dismiss();
                    if(downLoadListener!= null)
                        downLoadListener.startDownLoad();
                    break;
            }
        }
    };

    public void onResume() {
        super.onResume();
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        getDialog().getWindow().setLayout((int) (dm.widthPixels*1), (int) (dm.heightPixels*0.45));
    }

    public void setDownLoadListener(DownLoadListener downLoadListener){
        this.downLoadListener  =downLoadListener;
    }

    public interface DownLoadListener{
        public void startDownLoad();
    }

}
